﻿#region File Description
//-----------------------------------------------------------------------------
// XmlHelper.cs
//
// Attack of the Crazy Pixels
// Copyright (c) 2009 VosSoft
//-----------------------------------------------------------------------------
#endregion
#region License
//-----------------------------------------------------------------------------
// The MIT License (MIT)
//
// Copyright (c) 2009 VosSoft
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace Attack_of_the_Crazy_Pixels
{
    /// <summary>
    /// Helper class for XML operations.
    /// </summary>
    public static class XmlHelper
    {
        /// <summary>
        /// Reads a color node in the format <color red="255" green="255" blue="255" alpha="255"/>.
        /// </summary>
        /// <param name="node">The node containing the RGBA tags.</param>
        /// <param name="defaultColor">The default color if the node doesn't exists. Can be null.</param>
        /// <returns>The new color, the default value or null.</returns>
        public static Color? ReadColor(XmlNode node, Color? defaultColor)
        {
            if (node != null)
            {
                return new Color(byte.Parse(node.Attributes["red"].Value),
                    byte.Parse(node.Attributes["green"].Value),
                    byte.Parse(node.Attributes["blue"].Value),
                    byte.Parse(node.Attributes["alpha"].Value));
            }
            return defaultColor;
        }

        /// <summary>
        /// Reads a Vector2 node (the can be format <size width="0.0" height="0.0"/> or <vec x="0.0" x="0.0"/> etc.).
        /// </summary>
        /// <param name="node">The node containing the two vector tags.</param>
        /// <param name="defaultVector">The default vector if the node doesn't exists. Can be null.</param>
        /// <returns>The new Vector2, the default value or null.</returns>
        public static Vector2? ReadVector2(XmlNode node, Vector2? defaultVector)
        {
            if (node != null)
            {
                return new Vector2(float.Parse(node.Attributes[0].Value),
                    float.Parse(node.Attributes[1].Value));
            }
            return defaultVector;
        }

        /// <summary>
        /// Reads an node attribute.
        /// </summary>
        /// <typeparam name="T">The type to read (should be a value type).</typeparam>
        /// <param name="node">The node containing the attribute.</param>
        /// <param name="attributeName">The name of the attribute.</param>
        /// <param name="defaultValue">The default value if the node or the attribute doesn't exists.</param>
        /// <returns>The attribute value or the default value.</returns>
        public static T ReadAttribute<T>(XmlNode node, string attributeName, T defaultValue)
        {
            if (node != null && node.Attributes[attributeName] != null)
            {
                return (T)Convert.ChangeType(node.Attributes[attributeName].Value, typeof(T));
            }
            return defaultValue;
        }
    }
}
